import QtQuick 2.15
import QtQuick.Controls 2.15
import '../../../controls'
//售货--售货
Rectangle {
    anchors.fill: parent
    gradient: mainGradient
    property bool returnGoods: false //标记是否是退货
    focus: true
    Component.onCompleted: {

    }
    ListModel{
        id:lvData
    }

    function appendProduct(data){
        let tdata=data
        tdata['count']=1
        for(let i=0;i<lvData.count;i++)
        {
            let item=lvData.get(i)
            if(item.code === tdata.code)
            {
                let c=item.count
                if(!returnGoods && c + 1 > tdata.num)
                {
                    return
                }

                lvData.setProperty(i,'count',c + 1)
                return
            }
        }

        lvData.append(tdata)
    }

    Connections{
        target: ScannerHelper
        function onRecvScannerData(data){
            var request = HttpHelper.createIMRequest();
            request.onSucceed( function(request){
                appendProduct(request.responseData)
            } );
            request.onError(  function(request){
                CGlobal.showMessage(window,request.errorMessage)
            } );
            request.get("/sys/watpc/pc/water_product_search/" + data)
        }
    }

    Item {
        id:itemTop
        width: parent.width
        height: 80
        Row{
            spacing: 10
            anchors.centerIn: parent
            Rectangle{
                width: 500
                height: 43
                color: '#ffffff'
                radius:5
                TextField{
                    id:textKey
                    x:50
                    height: parent.height-2
                    width: parent.width - x - 4
                    font.pixelSize: 14
                    maximumLength:30
                    color: '#333333'
                    focus: true
                    placeholderText: qsTr('请输入扫描商品条形码')
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    background: Item{}
                    Keys.onReturnPressed: {
                       btnQuery.clicked()
                    }
                    Keys.onEnterPressed: {
                       btnQuery.clicked()
                    }
                    // onEditingFinished: {
                    //     console.log('------a---------')
                    // }
                }
            }

            Item {
                width: 50
                height: 1
            }
            Button{
                id:btnQuery
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#EA9508'
                    Text {
                        anchors.centerIn: parent
                        color: '#ffffff'
                        text: qsTr("确认")
                    }
                }
                onClicked: {
                    if( textKey.text.length === 0)
                    {
                        return
                    }

                    var request = HttpHelper.createIMRequest();
                    //                    request.enabledLog=true
                    request.onSucceed( function(request){
                        console.log('----water_product_search---',JSON.stringify(request.responseData))
                        appendProduct(request.responseData)
                    } );
                    request.onError(  function(request){
                        CGlobal.showMessage(window,request.errorMessage)
                    } );
                    request.get("/sys/watpc/pc/water_product_search/" + textKey.text)
                    textKey.clear()
                }
            }
        }
    }
    Rectangle {
        border.color: '#dddddd'
        anchors.fill: parent
        anchors.topMargin: 100
        anchors.leftMargin: 20
        anchors.bottomMargin: 20
        anchors.rightMargin: 20
        Rectangle {
            id:itemHeader
            width: parent.width - 2
            anchors.horizontalCenter: parent.horizontalCenter
            height: 40
            property var itemWidth: width / 4
            color: '#F5F7FA'
            Row{
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height
                width: parent.width
                Repeater{
                    model: [qsTr('商品名称'),qsTr('数量'),qsTr('单价'),qsTr('小计')]
                    Text {
                        width: itemHeader.itemWidth
                        height: itemHeader.height
                        color: '#E0000000'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: modelData
                        elide: Text.ElideRight
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }
            }
        }
        ListView{
            id:lvs
            anchors.fill: parent
            anchors.topMargin: itemHeader.height
            anchors.bottomMargin: 60
            model: lvData
            ScrollBar.vertical: ScrollBar{}
            clip: true
            property var itemWidth: width / 4
            property var itemHeight: 120
            boundsBehavior: Flickable.StopAtBounds
            delegate: Rectangle{
                width: lvs.width
                height: lvs.itemHeight
                color: index % 2 === 0 ? '#FFF2F2F2' : '#ffffff'
                Row{
                    Item {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        Column{
                            leftPadding: 20
                            anchors.verticalCenter: parent.verticalCenter
                            spacing: 4
                            Text {
                                color: '#333333'
                                text: name
                            }
                            Text {
                                color: '#666666'
                                text: specs
                            }
                            Text {
                                color: '#666666'
                                text: code
                            }
                        }
                    }
                    Item {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        Row{
                            spacing: 10
                            anchors.verticalCenter: parent.verticalCenter
                            Button{
                                width: 30
                                height: 30
                                text: '-'
                                onClicked: {
                                    if(count <= 1)
                                    {
                                        return
                                    }
                                    let less=count-1
                                    lvData.setProperty(index,'count',less)
                                }
                            }
                            Text {
                                text: count
                                anchors.verticalCenter: parent.verticalCenter
                            }
                            Button{
                                width: 30
                                height: 30
                                text: '+'
                                onClicked: {
                                    let less=count+1
                                    if(!returnGoods && less > num)
                                    {
                                        return
                                    }
                                    lvData.setProperty(index,'count',less)
                                }
                            }
                        }
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: '￥'+ price.toFixed(2)
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: '￥'+ (count * price).toFixed(2)
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }



                }
                Button {
                    width: 30
                    height: 30
                    anchors.right: parent.right
                    anchors.rightMargin: 20
                    anchors.verticalCenter: parent.verticalCenter
                    background: Image {
                        anchors.centerIn: parent
                        source: "qrc:/images/delete.png"
                    }
                    onClicked: {
                        if(!CommonHelper.question(window,qsTr('确定要删除商品吗?')))
                        {
                            return
                        }
                        lvData.remove(index)
                    }
                }
            }
        }
        Item {
            width: parent.width
            height: 60
            anchors.bottom: parent.bottom
            Row{
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                anchors.rightMargin: 20
                spacing: 10
                Text {
                    font.pixelSize: 24
                    font.bold: true
                    color: '#333333'
                    text: qsTr("合计:")
                    anchors.verticalCenter: parent.verticalCenter
                }
                Text {
                    font.pixelSize: 24
                    font.bold: true
                    color: '#E42727'
                    text: {
                        let all=0
                        for(let i=0;i<lvData.count;i++)
                        {
                            let item=lvData.get(i)
                            all+=item.count * item.price
                        }
                        return qsTr("￥")+all.toFixed(2)+qsTr('元')
                    }
                    anchors.verticalCenter: parent.verticalCenter
                }
                Item {
                    width: 10
                    height: 10
                }
                Button{
                    width: 120
                    height: 40
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("清空商品")
                        }
                    }
                    onClicked: {
                        if(!CommonHelper.question(window,qsTr('确定要清空吗?')))
                        {
                            return
                        }
                        lvData.clear()
                    }
                }
                Item {
                    width: 10
                    height: 10
                }
                Button{
                    width: 120
                    height: 40
                    background: Rectangle{
                        radius: 10
                        color: '#319B0B'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text:returnGoods ? qsTr("确定退货"): qsTr("确定售卖")
                        }
                    }
                    onClicked: {
                        if(lvData.count === 0)
                        {
                            return
                        }
                        CGlobal.createObjectAndOpen(window,'qrc:/main/InputBraceletNoDialog.qml',
                                                    {
                                                        isAddPage:false,
                                                        qmlPath:'qrc:/main/cashier/Sales/PlaceAnOrderDialog.qml',
                                                        properties:{returnGoods:returnGoods,goodData:lvData}
                                                    })
                        //CGlobal.createObjectAndOpen(window,'qrc:/main/cashier/Sales/PlaceAnOrderDialog.qml',{returnGoods:returnGoods,goodData:lvData})
                    }
                }
            }
        }
    }
}
